package utils

import (
	"eag_arch_layout/internal/model/VO"
	"fmt"
	"gopkg.in/yaml.v3"
	"os"
	"reflect"
)

func CreateYamlFile[T any](fileName string) error {
	config := make([]*VO.UITableColumn, 0)
	typeInfo := reflect.TypeFor[T]()
	for i := 0; i < typeInfo.NumField(); i++ {
		field := typeInfo.Field(i)
		tag := field.Tag.Get("json")
		if tag != "" {
			config = append(config, &VO.UITableColumn{
				Label:    field.Name,
				Width:    100,
				OrderSeq: uint32(i + 1),
				IsFilt:   false,
				Bg:       "",
				Prop:     tag,
			})
		}
	}
	// 创建文件
	file, err := os.Create(fileName + ".yaml")
	if err != nil {
		fmt.Println("Error creating file:", err)
		return err
	}
	defer file.Close()

	// 创建编码器
	encoder := yaml.NewEncoder(file)

	// 将配置编码为 YAML 数据
	err = encoder.Encode(&config)
	if err != nil {
		fmt.Println("Error encoding YAML:", err)
		return err
	}

	fmt.Println("YAML data saved to output.yaml")
	return nil
}
